package com.gaga.console.core.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.gaga.common.constant.Constants;
import com.gaga.console.base.entity.PageParams;
import com.gaga.console.base.mybatisplus.LambdaQueryWrapperExt;
import com.gaga.console.base.service.impl.SuperServiceImpl;
import com.gaga.console.core.entity.SysDatabase;
import com.gaga.console.core.mapper.SysDatabaseMapper;
import com.gaga.console.core.service.SysDatabaseService;
import com.gaga.metadata.driver.Driver;
import com.gaga.metadata.driver.DriverConfig;
import com.gaga.metadata.entity.database.DataBaseSchema;
import com.gaga.metadata.entity.database.DataBaseTable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liujia
 * @since 2022-06-01
 */
@Service
public class SysDatabaseServiceImpl extends SuperServiceImpl<SysDatabaseMapper, SysDatabase> implements SysDatabaseService {

    @Override
    public boolean testConnection(Long id) {
        SysDatabase sysDatabase = this.baseMapper.selectById(id);
        if(sysDatabase != null){
            String healthy = getDriver(sysDatabase).test();
            if(healthy.equals(Constants.HEALTHY)) {
                return true;
            }
            return false;
        }
        return false;
    }

    @Override
    public List<DataBaseTable> getTableListBySchema(PageParams<SysDatabase> data) {
        List<DataBaseTable> tableList = new ArrayList<>();
        LambdaQueryWrapperExt<SysDatabase> wraps = new LambdaQueryWrapperExt<>();
        data.buildSpecWrap(wraps);
        SysDatabase sysDatabase = this.baseMapper.selectOne(wraps);
        if(sysDatabase != null) {
//            tableList.addAll(getDriver(sysDatabase).listTables(data.getQueryParams().get("schemaName").toString()));
        }
        return tableList;
    }

    @Override
    public List<DataBaseTable> getTableList(Long id) {
        List<DataBaseTable> tableList = new ArrayList<>();
        SysDatabase sysDatabase = this.baseMapper.selectById(id);
        if(sysDatabase != null) {
            List<DataBaseSchema> schemasAndTables = getDriver(sysDatabase).getSchemasAndTables();
            schemasAndTables.forEach(schema -> tableList.addAll(schema.getTables()));
        }
        return tableList;
    }

    @Override
    public List<DataBaseSchema> getSchemaList(Long id) {
        List<DataBaseSchema> schemaList = new ArrayList<>();
        SysDatabase sysDatabase = this.baseMapper.selectById(id);
        if(sysDatabase != null) {
            schemaList.addAll(getDriver(sysDatabase).listSchemas());
        }
        return schemaList;
    }

    @Override
    public List<DataBaseSchema> getSchemaAndTableList(Long id) {
        List<DataBaseSchema> schemasAndTables = new ArrayList<>();
        SysDatabase sysDatabase = this.baseMapper.selectById(id);
        if(sysDatabase != null) {
            schemasAndTables.addAll(getDriver(sysDatabase).getSchemasAndTables());
        }
        return schemasAndTables;
    }

    private Driver getDriver(SysDatabase sysDatabase){
        DriverConfig config = new DriverConfig();
        BeanUtil.copyProperties(sysDatabase,config);
        Driver build = Driver.build(config);
        return build;
    }
}
